package com.simou.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.simou.common.BaseContext;
import com.simou.common.R;
import com.simou.entity.ShoppingCart;
import com.simou.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车：{}",shoppingCart);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 若为菜品
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(shoppingCart.getDishId()!=0,ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        else {     // 若为套餐
            queryWrapper.eq(shoppingCart.getSetmealId()!=0,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 取出用户ID
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne!=null){  // 若购物车已存在此菜品/套餐
            int number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }
        else{      // 若购物车不存在此菜品/套餐
            shoppingCart.setNumber(1);  //数量默认为1
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        // 若为套餐
        return R.success(cartServiceOne);

    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCartList( ){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        return R.success(shoppingCartService.list(queryWrapper));
    }

    @DeleteMapping("/clean")
    public R<String> deleteShoppingCartList(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);
        return R.success("删除购物车成功");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> reduceShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车减少页面:{}",shoppingCart);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId()!=null){    // 删除菜品
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        else{   // 删除套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        // 数量减1
        int number = cartServiceOne.getNumber();
        if (number!=0){
            number -= 1;
        }
        cartServiceOne.setNumber(number);
        // 更新shoppingCart表
        shoppingCartService.updateById(cartServiceOne);

        return R.success(cartServiceOne);
    }
}
